Skip to content

ffi: throw on missing memory helper arguments - #65500

Merged
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
soulee-dev:ffi-throw-on-missing-arguments
Sep 6, 2026
Merged

ffi: throw on missing memory helper arguments#65500
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
soulee-dev:ffi-throw-on-missing-arguments

Conversation

@soulee-dev

@soulee-dev soulee-dev commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

ffi.getInt8() through ffi.getFloat64(), ffi.setInt8() through ffi.setFloat64(), ffi.toBuffer() and ffi.toArrayBuffer() return undefined instead of throwing when a required argument is omitted, so a call that read or wrote nothing cannot be told apart from one that read a zero byte.

GetValidatedPointerAddress() and GetValidatedSize() already reject the same argument when it is passed explicitly as undefined. The args.Length() test in front of them short-circuits the call and returns Nothing without scheduling an exception. These six are the only tests in src/ where args.Length() can skip a call that throws; the only other Length() tests that guard a call at all guard Buffer::HasInstance(), which cannot throw.

$ # before
$ node --experimental-ffi -p "require('node:ffi').getUint8()"
undefined

$ # after
$ node --experimental-ffi -p "require('node:ffi').getUint8()"
TypeError: The pointer must be a bigint

Drop those tests. FunctionCallbackInfo::operator[] returns Undefined for an out-of-range index, which is exactly the value these validators reject, so each missing argument now produces the error its explicit undefined counterpart produces. The tests that guard an explicit throw, such as the ones in ToString() and GetRawPointer(), are left alone.

ExportBytes() carried the same two tests. They are unreachable through the public API because exportBytes is not exported and its three callers all validate len in JavaScript first, but they are the same shape.

No documentation change is needed: doc/api/ffi.md already lists these arguments as required.

Fixes: #65499
Refs: #62072
Refs: #62858
Refs: #65842

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/ffi

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels Aug 23, 2026
@codecov

codecov Bot commented Aug 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.66667% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.19%. Comparing base (7aba44d) to head (7ce4938).
⚠️ Report is 11 commits behind head on main.

Files with missing lines Patch % Lines
src/ffi/data.cc 66.66% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #65500      +/-   ##
==========================================
- Coverage   90.20%   90.19%   -0.02%     
==========================================
  Files         771      771              
  Lines      264622   264619       -3     
  Branches    50228    50240      +12     
==========================================
- Hits       238704   238660      -44     
- Misses      16907    16947      +40     
- Partials     9011     9012       +1     
Files with missing lines Coverage Δ
src/ffi/data.cc 77.55% <66.66%> (+1.83%) ⬆️

... and 30 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread src/ffi/data.cc
Comment thread test/ffi/test-ffi-memory.js Outdated
ffi.getInt8() through ffi.getFloat64(), ffi.setInt8() through
ffi.setFloat64(), ffi.toBuffer() and ffi.toArrayBuffer() return
undefined instead of throwing when a required argument is omitted, so a
call that read or wrote nothing cannot be told apart from one that read
a zero byte. All 22 helpers behave this way.

GetValidatedPointerAddress() and GetValidatedSize() already reject the
same argument when it is passed explicitly as undefined. The
args.Length() test in front of them short-circuits the call and returns
Nothing without scheduling an exception. These six are the only tests in
src/ where args.Length() can skip a call that throws; the only other
Length() tests that guard a call at all guard Buffer::HasInstance(),
which cannot throw. The remaining tests in this file guard an inline
predicate and throw in the branch, which is why setUint8(ptr) reports
"Expected an offset argument" while setUint8() reports nothing at all.

Drop those tests. FunctionCallbackInfo::operator[] returns Undefined for
an out-of-range index, which is exactly the value these validators
reject, so each missing argument now produces the error its explicit
undefined counterpart produces. The documentation already describes this
behavior: the signatures are ffi.getInt8(pointer[, offset]),
ffi.setInt8(pointer, offset, value) and
ffi.toBuffer(pointer, length[, copy]), and the getters are documented to
return a number or a bigint.

ExportBytes() carried the same two tests. They are unreachable through
the public API because exportBytes is not exported and its three callers
all validate len in JavaScript first, but they are the same shape.

Signed-off-by: Soul Lee <alus20x@gmail.com>
@soulee-dev

Copy link
Copy Markdown
Contributor Author

Opened the follow-up PR: #65842.

It switches the two type-check branches to ERR_INVALID_ARG_TYPE and is stacked on this PR, so it will need a rebase once this one lands.

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina mcollina added the request-ci Add this label to start a Jenkins CI on a PR. label Sep 6, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 6, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@gurgunday gurgunday left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@gurgunday gurgunday added the author ready PRs with CI started, the required approvals, and no outstanding review comments. label Sep 6, 2026
@daeyeon daeyeon added the commit-queue PRs queued for automated landing through the Commit Queue. label Sep 6, 2026
@nodejs-github-bot nodejs-github-bot added commit-queue-failed PRs whose Commit Queue landing failed and need manual intervention before retrying. and removed commit-queue PRs queued for automated landing through the Commit Queue. labels Sep 6, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Commit Queue failed

   ✘  GitHub CI is still running
   ✘  Last Jenkins CI still running

The pull request was removed from the Commit Queue and labeled commit-queue-failed PRs whose Commit Queue landing failed and need manual intervention before retrying. . After resolving the failure, remove that label and add commit-queue PRs queued for automated landing through the Commit Queue. to retry.

Full Commit Queue output
- Loading data for nodejs/node/pull/65500
✔  Done loading data for nodejs/node/pull/65500
----------------------------------- PR info ------------------------------------
Title      ffi: throw on missing memory helper arguments (#65500)
Author     Soul Lee <me@soulee.dev> (@soulee-dev)
Branch     soulee-dev:ffi-throw-on-missing-arguments -> nodejs:main
Labels     c++, author ready, needs-ci, commit-queue
Commits    1
 - ffi: throw on missing memory helper arguments
Committers 1
 - Soul Lee <alus20x@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/65500
Fixes: https://github.com/nodejs/node/issues/65499
Refs: https://github.com/nodejs/node/pull/62072
Refs: https://github.com/nodejs/node/pull/62858
Refs: https://github.com/nodejs/node/pull/65842
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/65500
Fixes: https://github.com/nodejs/node/issues/65499
Refs: https://github.com/nodejs/node/pull/62072
Refs: https://github.com/nodejs/node/pull/62858
Refs: https://github.com/nodejs/node/pull/65842
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
--------------------------------------------------------------------------------
   ℹ  This PR was created on Sun, 23 Aug 2026 07:19:54 GMT
   ✔  Approvals: 3
   ✔  - Matteo Collina (@mcollina) (TSC): https://github.com/nodejs/node/pull/65500#pullrequestreview-5124948444
   ✔  - Gürgün Dayıoğlu (@gurgunday): https://github.com/nodejs/node/pull/65500#pullrequestreview-5124974743
   ✔  - Daeyeon Jeong (@daeyeon): https://github.com/nodejs/node/pull/65500#pullrequestreview-5125150949
   ✘  GitHub CI is still running
   ℹ  Last Full PR CI on 2026-09-06T09:41:46Z: https://ci.nodejs.org/job/node-test-pull-request/77074/
- Querying data for job/node-test-pull-request/77074/
✔  Build data downloaded
   ✘  Last Jenkins CI still running
--------------------------------------------------------------------------------
   ✔  Aborted `git node land` session in /home/runner/work/node/node/.ncu

View workflow run

@daeyeon daeyeon removed the commit-queue-failed PRs whose Commit Queue landing failed and need manual intervention before retrying. label Sep 6, 2026
@trivikr trivikr added the commit-queue PRs queued for automated landing through the Commit Queue. label Sep 6, 2026
@trivikr

trivikr commented Sep 6, 2026

Copy link
Copy Markdown
Member

Re-added commit-queue since manual landing attempt noticed CI as successful

   ✔  Last GitHub CI successful
   ℹ  Last Full PR CI on 2026-09-06T11:27:34Z: https://ci.nodejs.org/job/node-test-pull-request/77074/
⠋ Querying data for job/node-test-pull-request/77074/   ✔  Last Jenkins CI successful
--------------------------------------------------------------------------------
✔ This PR should be ready to land, do you want to continue? Yes

If commit-queue fails again, it can be landed manually.

@nodejs-github-bot
nodejs-github-bot merged commit 7147a3e into nodejs:main Sep 6, 2026
84 checks passed
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Landed in 7147a3e

@nodejs-github-bot nodejs-github-bot removed the commit-queue PRs queued for automated landing through the Commit Queue. label Sep 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs with CI started, the required approvals, and no outstanding review comments. c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ffi: memory helpers return undefined instead of throwing when a required argument is omitted

7 participants